import java.util.*;

public class ShortestPath {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt(); // 城镇数量
        int m = scanner.nextInt(); // 道路数量

        int A = scanner.nextInt(); // 小丽所在城镇
        int B = scanner.nextInt(); // 小明所在城镇

        int C = scanner.nextInt(); // 传送门连接的城镇 C
        int D = scanner.nextInt(); // 传送门连接的城镇 D

        Map<Integer, List<int[]>> graph = new HashMap<>();

        for (int i = 0; i < m; i++) {
            int u = scanner.nextInt();
            int v = scanner.nextInt();
            int c = scanner.nextInt();

            graph.computeIfAbsent(u, k -> new ArrayList<>()).add(new int[]{v, c});
            graph.computeIfAbsent(v, k -> new ArrayList<>()).add(new int[]{u, c});
        }

        int shortestTime = dijkstra(graph, A, B, C, D);
        System.out.println(shortestTime);
    }

    private static int dijkstra(Map<Integer, List<int[]>> graph, int start, int end, int teleportA, int teleportB) {
        int n = graph.size();
        int[] dist = new int[n + 1];
        Arrays.fill(dist, Integer.MAX_VALUE);

        PriorityQueue<int[]> pq = new PriorityQueue<>(Comparator.comparingInt(a -> a[1]));
        pq.offer(new int[]{start, 0});

        dist[start] = 0;

        while (!pq.isEmpty()) {
            int[] current = pq.poll();
            int node = current[0];
            int distance = current[1];

            if (node == end) {
                return distance;
            }

            if (node == teleportA && dist[teleportB] > distance) {
                dist[teleportB] = distance;
                pq.offer(new int[]{teleportB, distance});
            }

            if (node == teleportB && dist[teleportA] > distance) {
                dist[teleportA] = distance;
                pq.offer(new int[]{teleportA, distance});
            }

            if (dist[node] < distance) {
                continue;
            }

            if (graph.containsKey(node)) {
                for (int[] neighbor : graph.get(node)) {
                    int nextNode = neighbor[0];
                    int weight = neighbor[1];

                    if (distance + weight < dist[nextNode]) {
                        dist[nextNode] = distance + weight;
                        pq.offer(new int[]{nextNode, dist[nextNode]});
                    }
                }
            }
        }

        return -1; // 如果无法从起点到达终点，返回-1表示不可达
    }
}
